package com.neusoft.javafxdemo2.controller;

import com.neusoft.javafxdemo2.common.FileUtils;
import com.neusoft.javafxdemo2.entity.Aqi;
import com.neusoft.javafxdemo2.entity.AqiFeedback;
import javafx.collections.FXCollections;
import javafx.collections.ObservableList;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.TableColumn;
import javafx.scene.control.TableView;
import javafx.scene.control.cell.PropertyValueFactory;
import javafx.scene.control.cell.TextFieldTableCell;

import java.util.List;
import java.util.Map;

public class SupervisorQueryAQIViewController {
    @FXML
    private TableView<AqiFeedback> tableView;
    @FXML
    private TableColumn<AqiFeedback,String> colAfId,colTelId,colProvinceName,
            colCityName,colAddress,colEstimateGrade,colDate,colInformation;
    private Map<String, List<AqiFeedback>> aqiFeedbackMap;
    private String telId;
    private ObservableList<AqiFeedback> aqiFeedBackList = FXCollections.observableArrayList();
    public void initialize(){
        tableView.setItems(aqiFeedBackList);
        colAfId.setCellValueFactory(new PropertyValueFactory<>("afId"));
        colTelId.setCellValueFactory(new PropertyValueFactory<>("telId"));
        colProvinceName.setCellValueFactory(new PropertyValueFactory<>("provinceName"));
        colCityName.setCellValueFactory(new PropertyValueFactory<>("cityName"));
        colAddress.setCellValueFactory(new PropertyValueFactory<>("address"));
        colEstimateGrade.setCellValueFactory(new PropertyValueFactory<>("estimateGrade"));
        colDate.setCellValueFactory(new PropertyValueFactory<>("date"));
        colInformation.setCellValueFactory(new PropertyValueFactory<>("information"));
        //从文件中查询数据，将数据添加到表格中
        aqiFeedbackMap = FileUtils.readObject("AqiFeedback.txt");
        telId = SupervisorLoginViewController.currentLoginSupervisor.getTelId();
        aqiFeedBackList.addAll(aqiFeedbackMap.get(telId));
        //设置表格允许编辑
        tableView.setEditable(true);
        //设置允许编辑列类型
        colAddress.setCellFactory(TextFieldTableCell.forTableColumn());
        colInformation.setCellFactory(TextFieldTableCell.forTableColumn());
        //处理编辑后的事件处理
        colAddress.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<AqiFeedback, String>>() {
            @Override
            public void handle(TableColumn.CellEditEvent<AqiFeedback, String> event) {
                //获取被编辑的行对象
                AqiFeedback aqiFeedback = event.getRowValue();
                aqiFeedbackMap.get(telId).remove(aqiFeedback);
                String newAddress = event.getNewValue();
                //将新地址值付给aqiFeedback对象，然后将这个对象更新到Map中，然后在重新写入文件
                aqiFeedback.setAddress(newAddress);
                aqiFeedbackMap.get(telId).add(aqiFeedback);
                FileUtils.writeObject("AqiFeedback.txt",aqiFeedbackMap);
            }
        });
        colInformation.setOnEditCommit(new EventHandler<TableColumn.CellEditEvent<AqiFeedback, String>>() {
            @Override
            public void handle(TableColumn.CellEditEvent<AqiFeedback, String> event) {
                //获取被编辑的行对象
                AqiFeedback aqiFeedback = event.getRowValue();
                aqiFeedbackMap.get(telId).remove(aqiFeedback);
                String newInformation = event.getNewValue();
                //将新地址值付给aqiFeedback对象，然后将这个对象更新到Map中，然后在重新写入文件
                aqiFeedback.setInformation(newInformation);
                aqiFeedbackMap.get(telId).add(aqiFeedback);
                FileUtils.writeObject("AqiFeedback.txt",aqiFeedbackMap);
            }
        });
    }
    public void delAqiFeedback(){
        AqiFeedback aqiFeedback = tableView.getSelectionModel().getSelectedItem();
        //将这个对象从List中删除，然后还需要将对象从Map中删除，然后重写写入文件
        aqiFeedBackList.remove(aqiFeedback);
        aqiFeedbackMap.get(telId).remove(aqiFeedback);
        FileUtils.writeObject("AqiFeedback.txt",aqiFeedbackMap);
    }
}
